package com.example.tool.action;

import com.example.tool.ui.HToolIcon;
import com.example.tool.utils.FileUtil;
import com.example.tool.utils.SwingUtil;
import com.intellij.openapi.actionSystem.ActionUpdateThread;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.ui.components.JBTextField;
import org.jetbrains.annotations.NotNull;

import javax.swing.JComponent;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

/**
 * <p>Title: ColumnActions</p >
 * Description: 自定义栏目
 * <p>CreateTime: 2022/10/24</p>
 * @author FT
 */
public class ColumnActions extends AnAction {

  @Override
  public @NotNull ActionUpdateThread getActionUpdateThread() {
    return super.getActionUpdateThread();
  }

  @Override
  public void actionPerformed(@NotNull AnActionEvent e) {
    String[] configInformation = ActionUtil.getInformation(e.getProject());
    if (configInformation != null) {
      String file = "/" + configInformation[2] + "/WEB-INF/jsp/" + configInformation[1] + "/column/";
      SwingUtil.setTips("ControllerBean名称需要填写：<span style=\"color: #F56C6C;\">/xxx.do</span> 格式<br>创建的 <span style=\"color: #F56C6C;\">前端文件</span> 将会放在以下路径中。<br><span style=\"color: #F56C6C;\">" + file + "</span>");
      ActionUtil.ActionCreatInput(e, jbTextFields -> {
        JComponent jbTextField0 = jbTextFields.get(0);
        JComponent jbTextField1 = jbTextFields.get(1);
        JComponent jbTextField2 = jbTextFields.get(2);
        String text0 = ((JBTextField) jbTextField0).getText().trim();
        String text1 = ((JBTextField) jbTextField1).getText().trim();
        String text2 = ((JBTextField) jbTextField2).getText().trim();
        if (text0.equals(text1)) {
          Messages.showMessageDialog(e.getProject(), "The 栏目类名 and ControllerBean名 Cannot be the same. Please re-enter!", "Error", HToolIcon.ModuleIcon);
          return false;
        }
        if (!(text2.startsWith("/") && text2.endsWith(".do"))) {
          Messages.showMessageDialog(e.getProject(), "The ControllerBean名 input format is incorrect. Please re-enter!", "Error", HToolIcon.ModuleIcon);
          return false;
        }
        return true;
      }, (information, value) -> {
        String ColumnClass = value.get(0);
        String ColumnControllerClass = value.get(1);
        String ColumnControllerBean = value.get(2);
        String ColumnFile = value.get(3);
        String ColumnFileMore = ColumnFile + "More";

        //xxColumn.java
        FileUtil.generateFile(information[0] + "/" + ColumnClass + ".java", "/demo/init/column/Column.txt",
            str -> str.replace("$package$", information[1])
                .replace("$ClassName$", ColumnClass)
                .replace("$TIME$", new SimpleDateFormat("yyyy-MM-dd").format(new Date()))
                .replace("$className$", FileUtil.lowerFirstCase(ColumnClass))
                .replace("$classBean$", ColumnControllerBean)
        );

        //xxColumnController.java
        FileUtil.generateFile(information[0] + "/" + ColumnControllerClass + ".java", "/demo/init/column/ColumnController.txt",
            str -> str.replace("$package$", information[1])
                .replace("$ClassName$", ColumnControllerClass)
                .replace("$TIME$", new SimpleDateFormat("yyyy-MM-dd").format(new Date()))
                .replace("$list$", "/" + configInformation[1] + "/colum/" + ColumnFile)
                .replace("$more$", "/" + configInformation[1] + "/colum/" + ColumnFileMore));

        String xmlPath = Objects.requireNonNull(e.getProject()).getBasePath() + "/" + configInformation[2] + "/WEB-INF/cfgHome/plugin/" + configInformation[1] + "/spring";

        // spring-xx-column.xml
        String fileColumnPath = xmlPath + "/spring-" + configInformation[1] + "-column.xml";
        FileUtil.generateFile(fileColumnPath, "/demo/spring/spring-demo.txt",
            str -> ActionUtil.appendNewBean(str, "/demo/base/column.txt", fileColumnPath, information, ColumnClass));

        // spring-xx-controller.xml
        String fileControllerPath = xmlPath + "/spring-" + configInformation[1] + "-controller.xml";
        FileUtil.generateFile(fileControllerPath, "/demo/spring/spring-demo.txt",
            str -> ActionUtil.appendNewControllerBean(str, fileControllerPath, information, new String[]{ColumnControllerClass, ColumnControllerBean}));


        String jspPath = Objects.requireNonNull(e.getProject()).getBasePath() + "/" + configInformation[2] + "/WEB-INF/jsp/" + configInformation[1] + "/column";

        //xx.jsp
        FileUtil.generateFile(jspPath + "/" + ColumnFile + ".jsp", "/demo/init/column/ColumnList.txt",
            str -> str.replace("$classBean$", ColumnControllerBean));

        //xxMore.jsp
        FileUtil.generateFile(jspPath + "/" + ColumnFileMore + ".jsp", "/demo/init/column/ColumnMore.txt",
            str -> str);

        VirtualFileManager.getInstance().refreshWithoutFileWatcher(false);
      }, "栏目类名: ", "Controller类名: ", "ControllerBean名：", "栏目前端文件：");
      SwingUtil.setTips(null);
    } else {
      Messages.showMessageDialog(
          e.getProject(),
          "The seeyon configuration file is required, but the corresponding file is not found or the file is incorrect, please reset it!",
          "Error",
          HToolIcon.ModuleIcon);
    }
  }

  @Override
  public void update(@NotNull AnActionEvent event) {
    ActionUtil.ActionP(event);
  }
}
